#include "../catch.hpp"

#include "common/comm/AgentManager.h"
#include "common/comm/Epoll.h"
#include "common/comm/TaskManager.h"

#include "common/log/LoggerFactory.h"

#include "common/http/HttpAgent.h"

#include "watchdog/TimerTestAgent.h"

int const EPOLLSIZE = 64;

HttpAgent *httpAgent;
SocketAddress servaddr;

TEST_CASE("HttpAgent") {
    SECTION("a") {
        Epoll::getInstance()->initialize(EPOLLSIZE);

        servaddr.setAddress("127.0.0.1", 2379);

        httpAgent =
            (AgentManager::getInstance())->createAgent<HttpAgent>(servaddr);
        httpAgent->init();

        // 设置定时器退出epoll
        TimerTestAgent *pTimer =
            (AgentManager::getInstance())->createAgent<TimerTestAgent>();
        struct timespec starttime, intervaltime;
        starttime.tv_sec = 1;
        starttime.tv_nsec = 0;
        intervaltime.tv_sec = 0;
        intervaltime.tv_nsec = 0;
        pTimer->init(starttime, intervaltime);

        // 启动epoll
        Epoll::getInstance()->run();

        // -------------资源回收-------------
        AgentManager::destroyInstance();
        TaskManager::destroyInstance();
        Epoll::destroyInstance();
        LoggerFactory::getInstance()->clear();
        LoggerFactory::destroyInstance();
    }
}